import 'package:flutter/material.dart';
import 'package:jd_flutter/base/foundation/flutter_extension.dart';
import 'package:jd_flutter/base/foundation/jd_device.dart';
import 'package:jd_flutter/base/widget/image/jd_network_image.dart';
import 'package:jd_flutter/modules/pages/question/models/question_model.dart';

class QuestionCell extends StatelessWidget {
  final QuestionItem item;
  const QuestionCell({super.key, required this.item});
/*
  String? id;
  String? nickName;
  String? headIcon;
  String? showText;
  List<String>? imageList;
 */
  @override
  Widget build(BuildContext context) {
    List<String> imageUrls = item.imageList ?? [];
    List<Widget> rows = [];
    int imagesCount = imageUrls.length;
    int imagesPerRow = imagesCount <= 4 ? 2 : 3;
    int numberOfRows = (imagesCount / imagesPerRow).ceil();

    double imageWH = JDDevice.width / imagesPerRow - 30;
    for (int i = 0; i < numberOfRows; i++) {
      List<Widget> rowChildren = [];
      for (int j = 0; j < imagesPerRow; j++) {
        int imageIndex = i * imagesPerRow + j;
        if (imageIndex < imagesCount) {
          rowChildren.add(Padding(
            padding: EdgeInsets.all(5),
            child: JDNetworkImage(
              url: imageUrls[imageIndex],
              fit: BoxFit.cover,
              width: imageWH,
              height: imageWH,
            ),
          ));
        }
      }
      rows.add(Row(children: rowChildren));
    }
    return Container(
      margin: const EdgeInsets.all(10),
      padding: const EdgeInsets.all(10),
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(9)),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              JDNetworkImage(
                url: item.headIcon ?? '',
                size: Size(40, 40),
                radius: 20,
              ),
              10.hGap,
              Text(item.nickName ?? ''),
            ],
          ),
          Padding(
            padding: EdgeInsets.symmetric(vertical: 10),
            child: Text(item.showText ?? ''),
          ),
          10.vGap,
          ...rows
        ],
      ),
    );
  }
}
